package com.md.mainpage.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.md.mainpage.R
import com.md.mainpage.utils.Utils
import com.md.network.api.Album
import com.md.network.api.Audio
import com.yxt.function_sdk_share.callback.SinglePopupCallBack
import com.yxt.function_sdk_share.constants.SINGLE_AUDIO_TYPE
import com.yxt.function_sdk_share.util.BottomPopupUtil
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.List
import kotlin.collections.indices
import kotlin.collections.set

const val TYPE_HEADER = 0x1
const val TYPE_ITEM = 0x2

class AlbumAdapter(var context: Context, var itemClick: ItemClick, var audios: List<Audio>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    lateinit var album: Album
    private var curPlayPos = -1
    var map = HashMap<String, Int>()

    init {
        fillMap(audios)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_HEADER) {
            var viewHeader = LayoutInflater.from(context).inflate(R.layout.adapter_album_header, parent, false)
            ViewHolderHeader(viewHeader)
        } else {
            var view = LayoutInflater.from(context).inflate(R.layout.adapter_album_new, parent, false)
            AlbumViewHolder(view)
        }

    }

    override fun getItemCount(): Int {
        return audios.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (position == 0) {
            (holder as ViewHolderHeader).apply {
                holder.tvPlayAll!!.text = context.resources.getString(R.string.main_play_all) + "${audios.size - 1}" //第一条是blank audio, 不是真实音频。这种写法不优雅，后续优化
                holder.ivPlay!!.setOnClickListener {
                    if (audios.size > 1) {
                        var curAudio = audios[1]
                        if (album != null && curAudio?.audioUrl != null) {
                            itemClick?.onItemClick(album, curAudio)
                        } else {
                            Toast.makeText(context, "数据异常", Toast.LENGTH_SHORT).show()
                        }
                    }

                }
                holder.ivSort!!.setOnClickListener {
                    val audioList = ArrayList<Audio>()
                    audioList.addAll(audios)
                    audioList.removeAt(0)
                    Utils.fillAudioData(album,audioList)
                    ARouter.getInstance().build("/album/dealonlinealbumaudios").withParcelableArrayList("SELECT_AUDIOS", audioList).withParcelable("SELF_ALBUM",album).navigation()
                }
            }
        } else {
            var curAudio = audios[position]
            var num = when {
                position < 10 -> "00$position"
                position in 10..99 -> "0$position"
                else -> position.toString()
            }
            (holder as AlbumViewHolder).apply {
                holder.tvNum!!.text = num
                holder.tvAuthor!!.text = curAudio.author
                holder.tvTitle!!.text = curAudio.name
                holder.cltRoot!!.setOnClickListener {
                    if (album != null && curAudio?.audioUrl != null) {
                        itemClick?.onItemClick(album, curAudio)
                    } else {
                        Toast.makeText(context, "数据异常", Toast.LENGTH_SHORT).show()
                    }
                }
                if (position == curPlayPos) {
                    holder.tvTitle!!.setTextColor(context.resources.getColor(R.color.maincolor_12C286))
                    holder.tvAuthor!!.setTextColor(context.resources.getColor(R.color.maincolor_12C286))
                } else {
                    holder.tvTitle!!.setTextColor(context.resources.getColor(R.color.maincolor_262626))
                    holder.tvAuthor!!.setTextColor(context.resources.getColor(R.color.maincolor_262626))
                }
                holder.fltIvMore!!.setOnClickListener{
                    var bottomType = arrayOf(SINGLE_AUDIO_TYPE.ADD)
                    BottomPopupUtil.shareWithWindows(context as Activity, bottomType,album.id,curAudio,null)
                }

            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            TYPE_HEADER
        } else {
            TYPE_ITEM
        }
    }

    public fun setData(album: Album?, audios: List<Audio>) {
        if (album != null) {
            this.album = album
        }
        this.audios = audios
        fillMap(audios)
        notifyDataSetChanged()
    }

    public fun setCurPlayAudio(audio: Audio) {
        map[audio.id]?.let {
            curPlayPos = it
            notifyDataSetChanged()
        }
    }

    private fun fillMap(list: List<Audio>) {
        list?.apply {
            for (pos in list.indices) {
                map[list[pos].id] = pos
            }
        }
    }

    class AlbumViewHolder : RecyclerView.ViewHolder {
        var tvNum: TextView? = null
        var tvTitle: TextView? = null
        var tvAuthor: TextView? = null
        var ivMore: ImageView? = null
        var cltRoot: ConstraintLayout? = null
        var fltIvMore:FrameLayout?=null

        constructor(itemView: View) : super(itemView) {
            tvNum = itemView.findViewById(R.id.tvNum)
            tvTitle = itemView.findViewById(R.id.tvTitle)
            tvAuthor = itemView.findViewById(R.id.tvAuthor)
            ivMore = itemView.findViewById(R.id.ivMore)
            cltRoot = itemView.findViewById(R.id.cltRoot)
            fltIvMore = itemView.findViewById(R.id.fltIvMore)
        }
    }

    class ViewHolderHeader : RecyclerView.ViewHolder {
        var tvPlayAll: TextView? = null
        var ivPlay: ImageView? = null
        var ivSort: ImageView? = null

        constructor(itemView: View) : super(itemView) {
            tvPlayAll = itemView.findViewById(R.id.tvPlayAll)
            ivPlay = itemView.findViewById(R.id.ivPlay)
            ivSort = itemView.findViewById(R.id.ivSort)
        }
    }

    open interface ItemClick {
        fun onItemClick(album: Album, curAudio: Audio)
    }
}